package com.lframework.xingyun.basedata.impl.make;

import com.lframework.starter.web.core.impl.BaseMpServiceImpl;
import com.lframework.xingyun.basedata.dto.make.ProductMakeDto;
import com.lframework.xingyun.basedata.entity.MakeMethod;
import com.lframework.xingyun.basedata.entity.ProductMake;
import com.lframework.xingyun.basedata.mappers.ProductMakeMapper;
import com.lframework.xingyun.basedata.service.make.MakeMethodService;
import com.lframework.xingyun.basedata.service.make.ProductMakeService;
import com.lframework.xingyun.basedata.vo.make.CreateProductMakeVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductMakeServiceImpl extends BaseMpServiceImpl<ProductMakeMapper, ProductMake> implements ProductMakeService {



    @Transactional(rollbackFor =  Exception.class)
    @Override
    public void create(String makeId, List<CreateProductMakeVo> vos) {
        lambdaUpdate().eq(ProductMake::getMakeId, makeId).remove();

        List<ProductMake> addBatch = new ArrayList<>();

        for (int i = 0; i < vos.size(); i++) {
            CreateProductMakeVo vo = vos.get(i);
            ProductMake productMake = new ProductMake();
            productMake.setMakeId(makeId);
            productMake.setCode(vo.getCode());
            productMake.setSkuCode(vo.getSkuCode());
            productMake.setOrderNo(i + 1);

            addBatch.add(productMake);
        }

        saveBatch(addBatch);

        getThis(MakeMethodService.class).lambdaUpdate()
                .set(MakeMethod::getRelationProducts, addBatch.size())
                .eq(MakeMethod::getId, makeId)
                .update();
    }

    @Override
    public List<ProductMakeDto> selectByMakeId(String makeId) {
        return getBaseMapper().selectByMakeId(makeId);
    }

    @Override
    public Boolean selectNormalFlag(String productId) {
        return getBaseMapper().selectNormalFlag(productId);
    }

}
